package com.dianhun.cn.config.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cyt
 * @create 2020-09-22 15:33
 */
@Slf4j
@RestControllerAdvice
public class ExConfig {

    @ExceptionHandler(value = Exception.class)
    public Map exception(Exception e, HttpServletRequest request) {
        String eMessage = e.getMessage();
        StringBuffer url = request.getRequestURL();
        System.out.println("ErrorMsg-->" + eMessage);
        System.out.println("url-->" + url);
        //打印日志
        log.info("错误信息-->" + eMessage + "===请求链接--->" + url);
        HashMap<Object, Object> hashMap = new HashMap<>(2);
        hashMap.put("msg", eMessage);
        hashMap.put("url", url);
        return hashMap;
    }
}
